package core;

import infrastructure.exceptions.operations.OperationException;
import inftrastructure.exceptions.ClientRuntimeException;

import java.io.Serializable;
import java.util.Iterator;

import remote.RemoteInvocation;
import server.IAdminInterface;
import dal.Pet;
import dal.Pet_type;
import dal.User;

public class AdminInterface extends ServerSession implements IAdminInterface{
	
	protected AdminInterface(long sessionId) {
		super(sessionId);	
	}

	private static final long serialVersionUID = -3671655399627376546L;

	@Override
	public void addUser(String username, String password,Boolean isAdministrator)
			throws OperationException {
		
		RemoteInvocation invoker;
		
		try {
			invoker = new RemoteInvocation(this.getClass(),
					this.getClass().getMethod("addUser", new Class<?>[]{String.class,String.class,Boolean.class}),
						getSessionId(), new Serializable[]{username,password,isAdministrator});
		} catch(Exception ex){
			throw new ClientRuntimeException("Bug !!!",ex);
		}	
		
		serverAccess.remoteConnection.invoke(invoker);
		
		
	}

	@Override
	public Iterator<User> getAllUsers() {
		RemoteInvocation invoker;
		
		try {
			invoker = new RemoteInvocation(this.getClass(),
					this.getClass().getMethod("getAllUsers", new Class<?>[]{}),
						getSessionId(), new Serializable[]{});
		} catch(Exception ex){
			throw new ClientRuntimeException("Bug !!!",ex);
		}	
		
		return serverAccess.remoteConnection.invoke(invoker);
	}

	@Override
	public void removeUser(String username) throws OperationException {
		RemoteInvocation invoker;
		
		try {
			invoker = new RemoteInvocation(this.getClass(),
					this.getClass().getMethod("removeUser", new Class<?>[]{String.class}),
						getSessionId(), new Serializable[]{username});
		} catch(Exception ex){
			throw new ClientRuntimeException("Bug !!!",ex);
		}	
		
		serverAccess.remoteConnection.invoke(invoker);	
		
	}

	@Override
	public void addPetType(Pet_type typeToAdd) throws OperationException {
		RemoteInvocation invoker;
		
		try {
			invoker = new RemoteInvocation(this.getClass(),
					this.getClass().getMethod("addPetType", new Class<?>[]{Pet_type.class}),
						getSessionId(), new Serializable[]{typeToAdd});
		} catch(Exception ex){
			throw new ClientRuntimeException("Bug !!!",ex);
		}	
		
		serverAccess.remoteConnection.invoke(invoker);
		
	}

	@Override
	public void removePetType(String typeToRemove) {
		RemoteInvocation invoker;
		
		try {
			invoker = new RemoteInvocation(this.getClass(),
					this.getClass().getMethod("removePetType", new Class<?>[]{String.class}),
						getSessionId(), new Serializable[]{typeToRemove});
		} catch(Exception ex){
			throw new ClientRuntimeException("Bug !!!",ex);
		}	
		
		serverAccess.remoteConnection.invoke(invoker);
		
	}
}
